package com.scu.qingshi.domain.email.service;

import com.scu.qingshi.domain.authorize.model.AuthorizationEmailMessage;
import com.scu.qingshi.infrastructure.utils.EmailUtil;
import com.scu.qingshi.infrastructure.utils.VerifyCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author qingshi
 */
@Service
public class VerifyCodeService {

    @Resource
    VerifyCodeUtil verifyCodeUtil;

    @Autowired
    EmailUtil emailUtil;

    public String sendVerifyCode(String email) {

        String code = verifyCodeUtil.getVerify(4, true);
        Date nowDate = new Date();
        Date expireDate = new Date(nowDate.getTime() + 60 * 1000);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String content = "您的验证码为" +
                "<h1>" + code + "</h1>" +
                "请在" + dateFormat.format(expireDate) + "之前使用";
        emailUtil.sendEmail(AuthorizationEmailMessage.FROM,
                email, AuthorizationEmailMessage.TITLE, content);

        return code;
    }

}
